package com.qf.service.caiwu_service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.Prefund;
import com.qf.bean.PrefundOrder;
import com.qf.bean.PrefundRule;
import com.qf.dao.caiwu_dao.PrefundMapper;
import com.qf.dao.caiwu_dao.PrefundOrderMapper;
import com.qf.dao.caiwu_dao.PrefundRuleMapper;
import com.qf.service.caiwu_service.PrefundOrderService;
import com.qf.utils.RandomUtil;
import com.qf.utils.caiwu.enums.PrefundBalanceStateEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 2019/12/13 0013
 * Author->DT
 * sassspringboot
 * 行到水穷处 坐看云起时
 * 模块：
 */
@Service
public class PrefundOrderServiceImpl implements PrefundOrderService {
    @Resource
    private PrefundOrderMapper prefundOrderMapper;
    @Resource
    private PrefundRuleMapper prefundRuleMapper;
    @Resource
    private PrefundMapper prefundMapper;
    @Override
    @Transactional
    public int insertSelective(PrefundOrder record) {
        int flag=0;
        //如果是预存操作：
        if ("预存".equals(record.getFundType())){
            //先设置赠送金额为0
            record.setGiveMoney(0.0);
            // 判断是否使用预存款规则:0表示不使用预存款规则
            if (record.getRid()!=0){
                //查询选择的对应预存款规则中的预存金额和赠送金额；
                PrefundRule prefundRule = prefundRuleMapper.selectByPrimaryKey(record.getRid());
                //计算赠送金额=页面输入的预存金额/规定预存金额*赠送金额
                Double giveMoeny=(record.getMoeny()/prefundRule.getPrefund())*prefundRule.getGiveFund();
                record.setGiveMoney(giveMoeny);
            }

            // 封装相应参数，保存在订单详情数据库
            //单据编号自动生成
            record.setReceiptNum(RandomUtil.dataRandom());
            //设置经办人
            record.setOperator("管理员");
            //保存预存订单表
            prefundOrderMapper.insertSelective(record);
            //查询对应pid的预存表的对象
            Prefund prefund = prefundMapper.selectByPrimaryKey(record.getPid());
            //计算最终存入的总金额=原总金额+预存金额+最终赠送金额
            Double totalMoney=prefund.getTotalMoney()+record.getMoeny()+record.getGiveMoney();
            prefund.setTotalMoney(totalMoney);
            //设置可用余额=原可用余额+预存金额+最终赠送金额
            prefund.setUsedBalance(prefund.getUsedBalance()+record.getMoeny()+record.getGiveMoney());
            //更新对应的客户的预存主表中的总金额和可用余额项；
            flag=prefundMapper.updateByPrimaryKeySelective(prefund);
        }
        // 如果是支出操作:
        else {
            //设置赠送金额为0；
            record.setGiveMoney(0.0);
            // 封装相应参数，保存在订单详情数据库
            //单据编号自动生成
            record.setReceiptNum(RandomUtil.dataRandom());
            //设置经办人
            record.setOperator("管理员");
            //保存支出订单表
            prefundOrderMapper.insertSelective(record);
            //查询对应pid的预存表的对象
            Prefund prefund = prefundMapper.selectByPrimaryKey(record.getPid());
            //预存款主表中可用余额减去支出的金额
            Double usedBalance=prefund.getUsedBalance()-record.getMoeny();
            //判断余额是否小于0
            if (usedBalance<0.0){
                // 小于0时修改余额状态为余额为零
                prefund.setUsedBalance(0.0);
                prefund.setBalanceState(PrefundBalanceStateEnum.ZERO_BALANCE.getState());
            }else {
                //大于0，修改余额状态
                prefund.setUsedBalance(usedBalance);
                prefund.setBalanceState(PrefundBalanceStateEnum.HAVE_BALANCE.getState());
            }
            //更新对应的客户的预存主表
            flag=prefundMapper.updateByPrimaryKeySelective(prefund);
        }
         return flag;
    }

    //预存款使用记录信息
    @Override
    public PageInfo<PrefundOrder> findPrefundOrderListByLimit(Map map) {
        //开启分页
        PageHelper.startPage((int)map.get("index"),(int)map.get("page"));
        //查询预存款信息列表
        List<PrefundOrder> list=prefundOrderMapper.findPrefundOrderList(map);
        //封装PageInfo
        return new PageInfo<>(list);
    }
}
